package com.shzu.jwc.ty.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.shzu.jwc.ty.bean.JudgeProblem;
import com.shzu.jwc.ty.bean.JudgeProblemExample;
import com.shzu.jwc.ty.common.ReadExcelJudge;
import com.shzu.jwc.ty.dao.JudgeProblemMapper;

@Service
public class JudgeProblemservice {

	@Autowired
	JudgeProblemMapper judgeProblemMapper;
	
	@Autowired
	ProblemClassService problemClassService;
	
	public List<JudgeProblem> getjugeproblem()
	{
		return judgeProblemMapper.selectByExample(null);
	}
	
	
	public List<JudgeProblem> getsearchjugeproblem(Integer problemClassId)
	{
		JudgeProblemExample example=new JudgeProblemExample();
		JudgeProblemExample.Criteria criteria=example.createCriteria();
		criteria.andProblemClassIdEqualTo(problemClassId);
		return judgeProblemMapper.selectByExample(example);
		
	}
	
	public Integer insertjudge(JudgeProblem judgeProblem)
	{
		JudgeProblemExample example=new JudgeProblemExample();
		JudgeProblemExample.Criteria criteria=example.createCriteria();
		criteria.andJudgeProblemTitleEqualTo(judgeProblem.getJudgeProblemTitle());
		if(judgeProblemMapper.countByExample(example)>0)
			return 1;
		else
			return judgeProblemMapper.insert(judgeProblem);
		
	}

	public void batchImport(String name,MultipartFile file) {
		ReadExcelJudge readExcelsc=new ReadExcelJudge();
		List<JudgeProblem> judgeProblems=readExcelsc.getExcelInfo(name, file);
		if(judgeProblems.size()!=0) {
			for(Integer i=0;i<judgeProblems.size();i++)
			{
				judgeProblems.get(i).setProblemClassId((problemClassService.valuegetid(judgeProblems.get(i).getJvalue())));
				judgeProblemMapper.insert(judgeProblems.get(i));
			}
		}
	}
	
	public Integer deletejudge(Integer judgeProblemId)
	{
		return judgeProblemMapper.deleteByPrimaryKey(judgeProblemId);
	}
	
	public Integer updatejudge(Integer id,String column,String value)
	{
		JudgeProblem judgeProblem=judgeProblemMapper.selectByPrimaryKey(id);
		if(column.equals("judgeProblemTitle"))
			judgeProblem.setJudgeProblemTitle(value);
		if(column.equals("judgeProblemRight"))
			judgeProblem.setJudgeProblemRight(Integer.parseInt(value));
		if(column.equals("problemClassId"))
			judgeProblem.setProblemClassId(Integer.parseInt(value));
		return judgeProblemMapper.updateByPrimaryKey(judgeProblem);
	}
}
